﻿/*
 * Created by SharpDevelop.
 * User: Administrator
 * Date: 2017-8-8
 * Time: 14:20
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;

namespace ProxyEditor
{
	/// <summary>
	/// Interaction logic for Kiaguan.xaml
	/// </summary>
	public partial class ToggleSwitchButton : UserControl
	{
		public ToggleSwitchButton()
		{
			InitializeComponent();
		}
		
		 public static readonly DependencyProperty IsCheckedProperty =
           DependencyProperty.Register("IsChecked", typeof(bool), typeof(ToggleSwitchButton), new PropertyMetadata(default(bool), OnIsCheckedChanged));
        public event RoutedEventHandler Checked;
        public event RoutedEventHandler UnChecked;
        public bool IsChecked
        {
            get { return (bool)GetValue(IsCheckedProperty); }
            set { SetValue(IsCheckedProperty, value); }
        }   
       
        
        private static void OnIsCheckedChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
        {
            (obj as ToggleSwitchButton).OnIsCheckedChanged(args);
        }
        
        private void OnIsCheckedChanged(DependencyPropertyChangedEventArgs args)
        {
            fillRectangle.Visibility = IsChecked ? Visibility.Visible : Visibility.Collapsed;
            slideBorder.HorizontalAlignment = IsChecked ? HorizontalAlignment.Right : HorizontalAlignment.Left;
            if (IsChecked && Checked != null)
            {
                Checked(this, new RoutedEventArgs());
            }
            if (!IsChecked && UnChecked != null)
            {
                UnChecked(this, new RoutedEventArgs());
            }
        }
        protected override void OnMouseLeftButtonUp(MouseButtonEventArgs args)
        {
            args.Handled = true;
            IsChecked ^= true;
            base.OnMouseLeftButtonUp(args);
        }
	}
}